﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.ApplicationCleaning;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Areas.Cleaning.Controllers
{
    public class CleaningProductController : BaseController
    {
        #region 清洗产品页面 + ActionResult CleaningProductIndex()
        public ActionResult CleaningProductIndex()
        {
            return View();
        }
        #endregion

        #region 清洗产品管理页面获取列表数据 + ActionResult CleaningProductList(CleaningProductModel model)
        /// <summary>
        /// 清洗产品管理页面获取列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult CleaningProductList(CleaningProductModel model)
        {
            var total = 0;
            var list = ServiceFactory.CleaningProductServiceInstance.GetPagingList(model, out total).AsNoTracking()
                .Select(a=>new  {
                            a.ID,
                            a.ProductName,
                            a.ProductDescribe,
                            a.UnitInstruction,
                            a.Price,
                            a.IsAvailable,
                            a.LastModifyBy,
                            a.LastModifyTime,
                        }).ToList();
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 清洗产品编辑 + ActionResult CleaningProductEdit(int id)
        /// <summary>
        /// 清洗产品编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult CleaningProductEdit(int id)
        {
            ViewBag.ShowTitle = id > 0 ? "编辑清洗产品" : "添加清洗产品";
            var CleaningProduct = ServiceFactory.CleaningProductServiceInstance.LoadByID(id);
            return View(CleaningProduct);
        }

        #endregion

        #region 保存清洗产品 + ActionResult SaveCleaningProduct(CleaningProduct model)

        [HttpPost]
        [ValidateInput(false)]
        //保存清洗产品
        public ActionResult SaveCleaningProduct(CleaningProduct model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };

            var serv = ServiceFactory.CleaningProductServiceInstance;
            model.ProductName = model.ProductName.Trim();
            //新增
            if (model.ID == 0)
            {
                if (serv.IsExist(model.ProductName))
                {
                    responseResult.Message = "该清洗产品已经存在";
                }
                else
                {
                    model.CreateTime = DateTime.Now;
                    model.CreateBy = CurrentUser.UserAccount;
                    model.LastModifyBy = CurrentUser.UserAccount;
                    model.LastModifyTime = DateTime.Now;
                    var entity = serv.AddEntities(model);
                    responseResult.Success = entity.ID > 0;
                }
            }
            //修改
            else
            {
                var entity = serv.LoadByID(model.ID);
                if (entity != null)
                {
                    if (serv.IsExist( model.ProductName, model.ID))
                    {
                        responseResult.Message = "已存在同名的清洗产品，请修改清洗产品名称";
                    }
                    //entity = ReflectHelper<CleaningProduct, CleaningProduct>.CloneT1ToT2(model, entity);
                    entity.ProductName = model.ProductName;
                    entity.Price = model.Price;
                    entity.UnitInstruction = model.UnitInstruction;
                    entity.ProductDescribe = model.ProductDescribe;
                    entity.IsAvailable = model.IsAvailable;
                    entity.LastModifyBy = CurrentUser.UserAccount;
                    entity.LastModifyTime = DateTime.Now;
                    responseResult.Success = ServiceFactory.CleaningProductServiceInstance.UpdateEntities(entity);
                }
                else
                {
                    responseResult.Message = "该清洗产品不存在或已被删除";
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.Success ? RedirectToAction("CleaningProductIndex") : RedirectToAction("CleaningProductEdit", new { id = model.ID });
        }


        #endregion

        #region 禁用/启用清洗产品 + JsonResult UpdateStatus(int id, bool isAvailable)
        /// <summary>
        /// 禁用/启用清洗产品
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="isAvailable">启用/禁用</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateStatus(int id, bool isAvailable)
        {
            var entity = ServiceFactory.CleaningProductServiceInstance.LoadByID(id);
            var success = false;
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                entity.LastModifyBy = CurrentUser.UserAccount;
                entity.LastModifyTime = DateTime.Now;
                success = ServiceFactory.CleaningProductServiceInstance.UpdateEntities(entity);
            }
            return Json(new
            {
                success = success,
                message = success ? "修改成功" : "修改失败"
            });
        }
        #endregion
    }
}